import time
import pytest
from appium import webdriver
from appium.options.android import UiAutomator2Options
from config import ANDROID_DESIRE_CAPS
from page.app_page_enter_home import AppPageEnterHome
from page.page_add import PageAdd
from page.page_login import PageLogin
from page.page_pay import PagePay
from page.page_search import PageSearch


@pytest.fixture(scope="function")
def driver():
    """app驱动的创建及退出"""
    # 创建app驱动对象
    des_caps = ANDROID_DESIRE_CAPS
    # 初始化设备配置信息
    option = UiAutomator2Options().load_capabilities(des_caps)
    # 创建驱动对象
    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=option)
    # 隐式等待
    driver.implicitly_wait(10)
    # 返回驱动对象
    yield driver
    # 暂停3秒
    time.sleep(3)
    # 关闭驱动
    driver.quit()
@pytest.fixture(scope="function",autouse=True)
def enter_home(driver):
    """全局：进入首页"""
    #  创建首页对象
    app_page_enter_home = AppPageEnterHome(driver)
    # 进入首页
    app_page_enter_home.page_swipe_enter_home()
@pytest.fixture
def login(driver):
    """全局：登录"""
    # 创建登录对象
    page_login = PageLogin(driver)
    # 登录
    page_login.login_input('13100001010','asd12345')
@pytest.fixture
def search(driver):
    """全局：搜索"""
    # 创建搜索对象
    page_search = PageSearch(driver)
    # 搜索
    page_search.search_inputgoods('华为手机')
@pytest.fixture
def add_cart(driver):
    """全局：添加购物车"""
    # 创建添加购物车对象
    page_add = PageAdd(driver)
    # 添加购物车
    page_add.add_goods()
@pytest.fixture
def pay(driver):
    """全局：支付"""
    # 创建支付对象
    page_pay = PagePay(driver)
    # 支付
    page_pay.buy_cart('asd12345')



